// pages/addTopic/addTopic.js
const http = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicName:'',
    topicSpe:'',
    hotTopicShow:false,
    hotTopicArr:[],
    topicText:'',
    isNiming: false, 
    hotShow:true,
    isClick: false
  },
  switch1Change(e){
    console.log(e.detail.value)
    this.setData({
      isNiming: e.detail.value
    })
  },
  //话题名称
  inputName(e){
    console.log(e.detail.value)
    this.setData({
      topicName: e.detail.value
    })
    

    
  },
  inputDes(e){
    console.log(e.detail.value)
    this.setData({
      topicSpe: e.detail.value
    })
  },

  //选择热门话题
  selectHotTopic(e){
    var topicText = e.currentTarget.dataset.topic.topic_name;
    var hotTopic = { id: e.currentTarget.dataset.topic.id, text: topicText}
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    if (prePage.route == "pages/fabudongtai/fabudongtai"){
      wx.setStorageSync('hotTopic', hotTopic)
    } else if (prePage.route == "pages/hotTopic/hotTopic"){

    }
    
    this.setData({
      hotTopicShow:true,
      topicText,
      topicId: hotTopic.id
    })
  },

  selectTopic(){
    if (!this.data.isclick){
      var uid = app.globalData.uid;
      var pages = getCurrentPages();
      var topicSpe = this.data.topicSpe;  //话题简介
      var topicText
      var isNiming = this.data.isNiming ? 1 : 0;
      if (this.data.hotTopicShow) {
        topicText = this.data.topicText
      } else {
        topicText = this.data.topicName
      }
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      if (prePage.route == "pages/fabudongtai/fabudongtai") {
        if (this.data.hotTopicShow) {
          wx.navigateBack({

          })
        } else {
          //发布话题
          wx.showLoading({title: '创建话题中请稍等...'})
          http.fetchApi('p_fahuati', { p: `R2020003|${uid}|${topicText}|${topicSpe}|${isNiming}` }).then(res => {
            wx.hideLoading();
            if (res.data.code == 1) {

              wx.showToast({
                title: '添加新话题',
              })
              var result = res.data.results[0]
              var hotTopic = { id: result.id, text: result.topic_name }
              wx.setStorageSync('hotTopic', hotTopic)
              // setTimeout(() => {
              wx.navigateBack({

              })

              // }, 1000)

            } else if (res.data.code == 0){
              wx.showToast({
                title: res.data.results,
                icon:'none'
              })
            }
          })

        }



      } else if (prePage.route == "pages/hotTopic/hotTopic") {
        //发布话题
        http.fetchApi('p_fahuati', { p: `R2020003|${uid}|${topicText}|${topicSpe}|${isNiming}` }).then(res => {
          if (res.data.code == 1) {
            // if (res.data.results[0].codes==1){
            wx.showToast({
              title: '成功发布话题',
            })

            setTimeout(() => {
              wx.navigateBack({

              })
              prePage.getTopicList(1)
            }, 1000)
            // }
          } else if (res.data.code == 0) {
            wx.showToast({
              title: res.data.results,
              icon: 'none'
            })
          }
        })
      }

    }else{
    return  wx.showToast({
        title: '请勿重复创建话题!',
        icon:'none'
      })
    }

    //防止多次点击
    this.setData({
      isclick: true
    })
   
  },
  //取消话题
  cancelTopic(){
    this.setData({
      hotTopicShow: false
    })
  },
  //获取热门话题
  getHotTopic() {
    http.fetchApi('procedure_t', { p: `R2019006` }).then(res => {
      if (res.data.code == 1) {
        var hotTopicArr = res.data.results;
        this.setData({
          hotTopicArr
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfor = app.globalData.userInfo;
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    if (prePage.route == "pages/fabudongtai/fabudongtai") {
     this.setData({
       hotShow:true
     })
    } else if (prePage.route == "pages/hotTopic/hotTopic") {
      //发布话题时无热门话题选项
      this.setData({
        hotShow: false
      })
    }

    this.setData({
      userInfor
    })
    this.getHotTopic();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})